#include <iostream>
using namespace std;
#include <unistd.h>
#include <pthread.h>
class Request//请求
{
    public:
    Request(int start,int end,const string &threadname)
    :start_(start),end_(end),threadname_(threadname)
    {

    }
    int total()
    {
        int sum=0;
        for(int i=start_;i<=end_;i++)
        {
            cout<<"count running!!!" <<i<<endl;
            usleep(10000);
            sum+=i;
        }
        return sum;
    }
    private:
    int start_;//开始计算的数字
    int end_;//计算到的结束数字
    string threadname_;//线程名
};
class Response//答复
{
    public:
    Response(int result)
    :result_(result)
    {

    }
    public:
    int result_;//答案，结果
};
void* Accumulation(void*arg)
{
    Request*request=static_cast<Request*>(arg);
    //把结果传出去
    Response* response=new Response(0);
    response->result_=request->total();
   //response request->total();
    //delete request;
    pthread_exit(response);
    cout<<"exit err !!"<<endl;
   return response;
}
int main()
{
    pthread_t tid;
    Request re1(1,100,"thread1");
    //创建一个线程计算1~100的相加
    pthread_create(&tid,nullptr,Accumulation,&re1);
   // pthread_cancel(tid);
   // cout<<"cancle success"<<endl;
    //等待线程
    void* retval;
    pthread_join(tid,&retval);//通过retval获取退出信息
    //提取退出信息
    Response* response1=static_cast<Response*>(retval);
    cout<<"result is : "<<response1->result_<<endl;
    delete response1;
    return 0;
}